package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCardService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCardServiceImpl implements ShoppingCardService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 添加的菜品是否在数据库已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 查询数据库
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        // 如果存在了的话数量加1即可
        if (list != null && list.size() > 0) {
            // 只会查询到一条数据
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            // 更新数据库
            shoppingCartMapper.updateNumberById(cart);
        } else {
            // 如果不存在，插入一条购物车数据

            // 判断本次添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 本次添加的是菜品
                // +因为要查询出来菜品的名称和图片路径所以引入mapper为这些字段赋值存入购物车表中
                Dish dish = dishMapper.getById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 本次添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setNumber(1); // 初始数量为1
            shoppingCart.setCreateTime(LocalDateTime.now());
        }

        // 插入购物车数据
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        // 获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().
                userId(userId).
                build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 减少购物车
     *
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 减少一个选择的商品，根据id查询，传回来的是菜品id +- 口味 或者套餐id
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .dishId(dishId)
                .setmealId(setmealId)
                .dishFlavor(dishFlavor)
                .build();

        // 根据菜品id和口味查询购物车数据(只可能查到一条数据)
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = shoppingCartList.get(0);

        // 判断购物车数量
        if (cart.getNumber() == 1) {
            // 数量大于1直接删除
            shoppingCartMapper.deleteById(cart.getId());
        } else {
            // 数量小于1修改-1
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
        }
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        // 获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        // 通过用户id清空购物车
        shoppingCartMapper.deleteByUserId(userId);
    }
}
